--
-- Author: ping.wang
-- Date: 2018-07-02 10:59:55
--
-- NewguildStudyPanel
--

local TabsBtn = require("app.widget.TabsBtn")
local NewguildHomeLeftRender = import(".renders.NewguildHomeLeftRender")
local NewguildTalentIconRender = import(".renders.NewguildTalentIconRender")

local ClassRef = app.mvc.createPanelClass(...)
function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

--每个节点周围关联的线
function ClassRef:init(data)
	self.super.init(self, data)
	self._guildHomeLeftRender = NewguildHomeLeftRender.create(self._ui.left)--界面左边部分 公会信息

	--公会技能点Tips
	self._guildSkillTips = self._ui.Panel_study:getChildByName("tips")

	display.uiAddClick( self._guildSkillTips, function()
        local posTemp = self._guildSkillTips:convertToWorldSpace(cc.p(0,0))
        local width, height = self._guildSkillTips:getContentSize2()
        posTemp.x = posTemp.x + width
        executeFunc(self._onOperate, "showTips_guildSkillPoint", posTemp )
    end )

	--节点的邻接点
	self._RelationNodeIdx={
	    {2,6},
	    {1,3,7},
	    {2,4,8},
	    {3,5,9},
	    {4,10},
	    {1,7,11},
	    {2,6,8,12},
	    {3,7,9,13},
	    {4,8,10,14},
	    {5,9,15},
	    {6,12},
	    {7,11,13},
	    {8,12,14},
	    {9,13,15},
	    {10,14}
	}

	--与邻接点相连接的线 一一对应
	self._LineIndexes={
		{1,13},
		{1,2,14},
		{2,3,15},
		{3,4,16},
		{4,17},
		{13,5,18},
		{14,5,6,19},
		{15,6,7,20},
		{16,7,8,21},
		{17,8,22},
		{18,9},
		{19,9,10},
		{20,10,11},
		{21,11,12},
		{22,12}
	}

	--刷新公会技能显示 默认选中第一个
	self._talentNodes = {}
	for i = 1, 15 do
		local talentNode = NewguildTalentIconRender.create(self._ui.Panel_study:getChildByName("talent_"..i))
		talentNode:setClickHandler(handler(self, self._clickTalentIcon))
		table.insert(self._talentNodes,talentNode)
	end

    self._talentNodeLines={}
    for i=1,22 do
        local talentNodeLine = self._ui.Panel_sens:getChildByName("line"..i)
        talentNodeLine:setVisible(false)
        table.insert(self._talentNodeLines,talentNodeLine)
    end

    --显示当前选中的技能icon
    self._selectedIcon = NewguildTalentIconRender.create(self._ui.Panel_bom:getChildByName("icon"))
    self._selectedIcon:setSelected(false)
    self._selectedTalentName = self._ui.Panel_bom:getChildByName("name")
    self._selectedTalentDesc = self._ui.Panel_bom:getChildByName("ScrollView"):getChildByName("text")


	self._activePanel = self._ui.Panel_open --激活panel
	self._setBtn = self._ui.Panel_bom:getChildByName("btn_set") --设置按钮 对于策划表中填写贡献度等级不是0的天赋 会长和精英玩家看到
	self._tipsBtn = self._ui.Panel_bom:getChildByName("btn_tips") --不满足贡献度等级的普通玩家将看到这个按钮
	self._tipsBtn:addClickEventListener(function()
		local posTemp = self._tipsBtn:convertToWorldSpace(cc.p(0,0))
		local width, height = self._tipsBtn:getContentSize2()
		posTemp.x = posTemp.x - width - 100
		posTemp.y = posTemp.y + 80
		executeFunc(self._onOperate, "show_guildtalent_donatelv_tips",{self._selectedTalentData:getNeedDonateLv(),self._myGuildMemberInfo:getDonateLv(),posTemp})
	end)


	self._activeBtn = self._activePanel:getChildByName("Button") --激活按钮
	self._activeMoneyBg =  self._activePanel:getChildByName("bg") --激活钱数的BG
	self._activeMoneyIcon = self._activePanel:getChildByName("icon") --激活金钱的ICON
	self._activeMoneyNum = self._activePanel:getChildByName("num") --激活金钱数量
	self._activeText = self._activePanel:getChildByName("Text_name") --激活文字
	self._activeTipsText = self._activePanel:getChildByName("tips") --激活提示文字

	--给激活按钮添加一个事件
	self._activeBtn:addClickEventListener(handler(self, self._activeTalent))
	self._activeBtn:setSwallowTouches(true)

	--设置按钮添加事件
	self._PanelMore = self._ui.Panel_more --显示设置
	local panelBtn = self._PanelMore:getChildByName("Panel_btn")
    local btns = {}
	for i = 1,10 do
        btns[i] = panelBtn:getChildByName("btn_"..i)
    end

    local function onTabsBtn(index, btn)
        self._curTalentDonateLv = index
    end

    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1)


	display.nodeRemoveForNotHit(self._PanelMore, function()
		if self._PanelMore:isVisible() then
			self._PanelMore:performWithDelay(function() 
				self._PanelMore:setVisible(false)
				self:_sendModifyDonateLvMsg(self._selectedTalentData:getId(), self._curTalentDonateLv)
				end, 0)
		end
	end)


	self._setBtn:addClickEventListener(handler(self, self._showSetDonateLvPanel))

    self:_unShowAllTallentNodes()
	
end

function ClassRef:onOperate( cb )
	self._onOperate = cb
	self._guildHomeLeftRender:onOperate(self._onOperate)
end

-- 刷新基础信息
function ClassRef:refreshGuildInfo( myGuildInfo, myGuildMemberInfo, myGuildTalent)
	self._guildHomeLeftRender:refreshGuildInfo(myGuildInfo)
	self._myGuildSkillPoint = myGuildInfo:getGuildLastSkillPoint() --公会剩余技能点
	self._guildSkillTips:setString(string.format( L("guild_skillpoint"), self._myGuildSkillPoint))--刷新技能点

	self._myGuildMemberInfo = myGuildMemberInfo --我的会员信息
	self._myGuildLv = myGuildInfo:getGuildLv() --我的公会等级
	self._myGuildMoney = myGuildInfo:getGuildFund() --公会资金


	self:_refreshTalent(myGuildTalent) --根据公会的天赋信息来显示图标
    self:_refreshSelectedTalentIcon() --刷新选择的图标
end

function ClassRef:_refreshTalent(myGuildTalent)
	for _,talentNodeLine in pairs(self._talentNodeLines) do
		talentNodeLine:getChildByName("img"):setVisible(false)
	end
	--此处根据表数据决定了显示哪些地方的技能图标
	for _,v  in ipairs(myGuildTalent) do
		local talentNode = self._talentNodes[v:getSeat()]

		if talentNode then
			talentNode:refreshWithData(v)
			if not self._selectedTalentData then
                self._selectedTalentData = talentNode:getData()
			end
		end
	end

	--初始化显示的线条
	for k1,talentNode in pairs(self._talentNodes) do
		local talentData = talentNode:getData()
		if talentNode:isVisible() and  talentData then
			--遍历该点周围的点
			local talentPreSeats = talentData:getProSeats()
			if table.nums(talentPreSeats) > 0 then
				for k2,nodeIndex in pairs(self._RelationNodeIdx[k1]) do
					--判断此点是否是他的前置节点 若是 则设置线路为可见
					if table.hasvalue(talentPreSeats, nodeIndex) then
						local tmpNode = self._talentNodes[nodeIndex]
						if tmpNode:isVisible() then
							local lineNode=self._talentNodeLines[self._LineIndexes[k1][k2]]
							lineNode:setVisible(true)
							--此处判断是否可以学习 或 激活
							if (talentNode:getData():canGet() or talentNode:getData():isActive()) and (tmpNode:getData():canGet() or tmpNode:getData():isActive()) then
								lineNode:getChildByName("img"):setVisible(true)
							end
						end
					end

				end
			end
		end
	end
end

-- 初始的时候隐藏选中天赋节点
function ClassRef:_unShowAllTallentNodes()
    for _,talentNode in ipairs(self._talentNodes) do
        talentNode:setVisible(false)
    end
    for _,talentNodeLine in ipairs(self._talentNodeLines) do
        talentNodeLine:setVisible(false)
    end
end

--根据当前选择的技能进行刷新
function ClassRef:_refreshSelectedTalentIcon()
    if self._selectedTalentData then
		local isManagerMember = self._myGuildMemberInfo:isGuildManager()
		local myDonateLv = self._myGuildMemberInfo:getDonateLv()

        self._selectedIcon:setIcon(self._selectedTalentData:getIconName())
		--普通玩家看到已激活但是未达到贡献度要求的技能图标上加锁
		if self._selectedTalentData:isActive() and not isManagerMember and self._selectedTalentData:getNeedDonateLv() > myDonateLv then
			self._selectedIcon:setLockVisible(true)
		else
			self._selectedIcon:setLockVisible(false)
		end

        self._selectedTalentDesc:setString(self._selectedTalentData:getTalentDes())

        for _,v in ipairs(self._talentNodes) do
            local pData = v:getData()
            if pData then
                v:setSelected(self._selectedTalentData:getId() == v:getData():getId())
				if pData:isActive() and not isManagerMember and pData:getNeedDonateLv() > myDonateLv then
					v:setLockVisible(true)
				else
					v:setLockVisible(false)
				end
            end
        end

        --根据玩家的状态显示
        self:_refreshBtnByMemberInfo()
    end
end

--按下技能图标触发
function ClassRef:_clickTalentIcon(talentNode)
	local isVisible = self._PanelMore:isVisible()

	if not isVisible and  talentNode and ( (not self._selectedTalentData) or self._selectedTalentData:getId() ~= talentNode:getData():getId() )then
		self._selectedTalentData = talentNode:getData() --持有当前选中的技能的数据信息
        self:_refreshSelectedTalentIcon()
	end
end

--根据我的会员信息刷新面板
function ClassRef:_refreshBtnByMemberInfo()
	if self._myGuildMemberInfo and self._selectedTalentData then
		local isManagerMember = self._myGuildMemberInfo:isGuildLeader() or self._myGuildMemberInfo:isGuildOfficer()
		local needDonateLv = self._selectedTalentData:getNeedDonateLv() --当前天赋需要的贡献度等级
		local curTalentIsActive = self._selectedTalentData:isActive() --当前显示的天赋是否已经激活
		local needGuildLv = self._selectedTalentData:getNeedGuildLv()
		if isManagerMember then
			--会长或者精英会员
            self._tipsBtn:setVisible(false)
            self._setBtn:setVisible(false)
			self._activePanel:setVisible(true)
			if curTalentIsActive then
				--如果技能是激活的
				--如果贡献等级需求为0则不用显示设置按钮 否则显示
				self._setBtn:setVisible(needDonateLv ~= 0)
				self._activePanel:setVisible(false)
			elseif self._selectedTalentData:canGet() then
				--如果技能前置技能都达标了 表名可以激活 但是从这里要判断需要的公会等级
				if needGuildLv > self._myGuildLv then
					--不可解锁
					self:_setActivePanel(needGuildLv, self._selectedTalentData:getCost(),false)
				else
					--可解锁
					self:_setActivePanel(nil, self._selectedTalentData:getCost(),false)
				end
			else
				--不可解锁
				self:_setActivePanel(needGuildLv, self._selectedTalentData:getCost(),true)
			end

		else
			--普通玩家
			self._activePanel:setVisible(false)
			self._setBtn:setVisible(false)
			if curTalentIsActive and self._myGuildMemberInfo:getDonateLv() < needDonateLv then
				self._tipsBtn:setVisible(true)
			else
				self._tipsBtn:setVisible(false)
			end
		end

		--天赋名字显示
		if curTalentIsActive then
			self._selectedTalentName:setString(string.format( L("guild_skillunlock"), self._selectedTalentData:getTalentName() ))
			self._selectedTalentName:setTextColor(cc.c4b(255, 225, 132, 255) )
			--普通成员若没有达到贡献等级需要显示不一样
			if not isManagerMember and self._myGuildMemberInfo:getDonateLv() < needDonateLv then
				self._selectedTalentName:setString(string.format( L("guild_memberlevelrequired"), self._selectedTalentData:getTalentName(), needDonateLv ))
			end

		else
			self._selectedTalentName:setString(string.format( L("guild_skilllock"), self._selectedTalentData:getTalentName() ))
			self._selectedTalentName:setTextColor(cc.c4b(179, 179, 177, 255) )
        end

	end
end


--设置激活按钮信息
function ClassRef:_setActivePanel(lockLv,unLockNeedMoney,preLock)
	if lockLv and lockLv ~= 0 then
		--无法激活 显示锁
		display.setBtnTouchEnable(self._activeBtn,false)
		self._activeMoneyBg:setVisible(false) --激活钱数的BG
		self._activeMoneyIcon:setVisible(false) --激活金钱的ICON
		self._activeMoneyNum:setVisible(false) --激活金钱数量
		self._activeText:setVisible(true) --激活文字
		self._activeTipsText:setVisible(true)--激活提示文字

		self._activeText:setString(L("guild_unlockrequired"))
		if preLock then
			self._activeTipsText:setString(L("guild_preSkillUnlock"))
		else
			self._activeTipsText:setString(string.format( L("guild_level"), lockLv))
		end
	else
		display.setBtnTouchEnable(self._activeBtn,true)
		self._activeMoneyBg:setVisible(true) --激活钱数的BG
		self._activeMoneyIcon:setVisible(true) --激活金钱的ICON
		self._activeMoneyNum:setVisible(true) --激活金钱数量
		self._activeText:setVisible(true) --激活文字
		self._activeTipsText:setVisible(false)--激活提示文字

		self._activeText:setString(L("guild_activate"))
		--判断此时公会的技能点是否足够 若足够正常显示 不足显示为红色
		if self._myGuildSkillPoint > 0 then
			self._activeText:setTextColor(cc.c3b( 255, 255, 255 ))
		else
			self._activeText:setTextColor(cc.c3b( 255, 105, 85 ))
		end

		self._activeMoneyNum:setString(tostring(unLockNeedMoney))

		if self._myGuildMoney < unLockNeedMoney then
			self._activeMoneyNum:setTextColor(cc.c3b( 255, 105, 85 ))
		else
			self._activeMoneyNum:setTextColor(cc.c3b( 255, 255, 255 ))
		end
	end
end

--激活按钮事件
function ClassRef:_activeTalent()
	executeFunc(self._onOperate, "study_guild_talent", {self._selectedTalentData:getCost(), self._selectedTalentData:getId()})
end

--显示设置贡献度界面
function ClassRef:_showSetDonateLvPanel()
	--因为点击空白会导致界面关闭 因此 这个按钮就只用执行打开方法
	local isVisible = self._PanelMore:isVisible()
	if not isVisible then
		self._PanelMore:setVisible(true)
		self._curTalentDonateLv = self._selectedTalentData:getNeedDonateLv()
        assert(self._curTalentDonateLv >= 1 and self._curTalentDonateLv <= 10)
        self._tabsBtn:setSelected(self._curTalentDonateLv, false)
	end
end

--准备发送设定贡献度等级消息
function ClassRef:_sendModifyDonateLvMsg(id, lv)
	if lv ~= self._selectedTalentData:getNeedDonateLv() then
		executeFunc(self._onOperate, "set_guild_talent_donatelv", {id, lv})
	end
end


return ClassRef